package com.general.framework.core.lang;

import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 集合常用工具类
 * @author leejean
 */
public abstract class Lists {

    public static <E> ArrayList<E> newArrayList() {
        return new ArrayList();
    }

    /**
     * 空集合
     * @param <E>
     * @return return final list
     */
    public static <E> List<E> emptyList() {
        return Collections.EMPTY_LIST;
    }

    /**
     * 判断一个集合是否为空或空集
     *
     * @param c 集合对象
     * @return true-空或空集
     */
    public static boolean isEmpty(final Collection c) {
        return c == null || c.isEmpty();
    }

    /**
     * 判断一个集合不为空
     *
     * @param c
     * @return
     */
    public static boolean isNotEmpty(final Collection c) {
        return !isEmpty(c);
    }

    /**
     * 根据一组可变参数对象构建array list
     *
     * @param objs 可变参数对象
     * @param <E>  item类型
     * @return
     */
    public static <E> List<E> asList(final E... objs) {
        final List<E> list = new ArrayList<>();
        if (objs != null && objs.length > 0) {
            for (final E obj : objs) {
                list.add(obj);
            }
        }
        return list;
    }

    /**
     * 拆分数据
     *
     * @param origin
     * @param size
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> divide(List<T> origin, int size) {
        if (Lists.isEmpty(origin)) {
            return emptyList();
        }
        int block = (origin.size() + size - 1) / size;
        return IntStream.range(0, block).boxed().map(i -> {
            int start = i * size;
            int end = Math.min(start + size, origin.size());
            return origin.subList(start, end);
        }).collect(Collectors.toList());
    }

    public static boolean contains(List dataList, Object data) {
        if (Lists.isEmpty(dataList)) {
            return false;
        }
        return ArrayUtils.contains(dataList.toArray(), data);
    }

}
